#include <iostream>
#include <string>
using namespace std;

#include <inlinehook.h>

#include <Windows.h>

//int WINAPI MessageBox(HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption,UINT uType);
MyDetourProc(has_Return, int, WINAPI, MessageBoxA, (
          HWND hWnd
        , LPCTSTR lpText
        , LPCTSTR lpCaption
        , UINT uType)) {
    std::string text  = "Dzlua notify : \n";
                text += lpText;
    return CallTrampoline(MessageBoxA)(
          hWnd
        , text.c_str()
        , lpCaption
        , uType );
}

//int WINAPI MessageBoxW(HWND hWnd,LPCWSTR lpText,LPCWSTR lpCaption,UINT uType);
MyDetourProc(has_Return, int, WINAPI, MessageBoxW, (
          HWND hWnd
        , LPCWSTR lpText
        , LPCWSTR lpCaption
        , UINT uType)) {
    std::wstring text  = L"Dzlua notify : \n";
                text += lpText;
    return CallTrampoline(MessageBoxW)(
          hWnd
        , text.c_str()
        , lpCaption
        , uType );
}

//----------------------------//
bool HookA() {
    ENABLE_HOOK(MessageBoxA);
    cout << "hookA ok." <<endl;
    return true;
}

bool HookW() {
    HMODULE hModule = LoadLibraryA("user32.dll");
    if (!hModule) return false;
    FARPROC realfunc = ::GetProcAddress(hModule, "MessageBoxW");
    if (!realfunc) return false;
    ENABLE_HOOK2(MessageBoxW, realfunc);
    cout << "hookW ok" <<endl;
    return true;
}

void UnhookA(){
    DISABLE_HOOK(MessageBoxA);
}

void UnhookW(){
    DISABLE_HOOK(MessageBoxW);
}

int main(int argc, char** argv)
{
    cout << "hello world!" << endl;
    ::MessageBoxA(0, "before hookA", "titleA", 0);
    HookA();
    ::MessageBoxA(0, "after hookA", "titleA", 0);
    UnhookA();
    ::MessageBoxA(0, "un hookA", "titleA", 0);

    //-----------//
    ::MessageBoxW(0, L"before hookW", L"titleW", 0);
    HookW();
    ::MessageBoxW(0, L"after hookW", L"titleW", 0);
    UnhookW();
    ::MessageBoxW(0, L"un hookW", L"titleW", 0);

    return 0;
}
